package mock

import (
	"context"

	"mall/common/jwt"
	"mall/common/resultx"
	"mall/common/utils/pointy"
	"mall/service/forum/api/internal/svc"
	"mall/service/forum/api/internal/types"
	"mall/service/user/rpc/userclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type LogoutLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewLogoutLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LogoutLogic {
	return &LogoutLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

// 退出登录
func (l *LogoutLogic) Logout(
	req *types.LogoutReq,
	headers *[]*types.Header,
) (resp *types.BaseMsgResp, err error) {
	userId := jwt.GetUserId(l.ctx)
	if userId == 0 {
		return nil, resultx.NewErrCode(resultx.TOKEN_EXPIRE_ERROR)
	}

	_, err = l.svcCtx.UserRpc.Logout(l.ctx, &userclient.LogoutReq{
		UserId: pointy.GetPointer(userId),
		Cancel: req.Cancel,
	})
	if err != nil {
		return nil, err
	}

	return &types.BaseMsgResp{
		Msg: "操作成功",
	}, nil
}
